package com.game;

import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.young.R;

public class MenuView extends RelativeLayout implements OnClickListener, OnTouchListener {
	private static final String TAG = MenuView.class.getSimpleName();
	
	private Context mContext;
	private OnButtonClickListener mOnButtonClickListener;
	
	RelativeLayout leftContent;
	ImageView btnClose;
	ImageView btnMain;
	ImageView btnReplay;
	ImageView btnMusic;
	ImageView btnSound;
	RelativeLayout RightContent;
	ListView lstvAds;
	AdsAdapter adapter;
	ArrayList<String> list;
	
	Animation animShow;
	Animation animHide;
	Animation animFadeIn;
	Animation animFadeOut;
	
	public MenuView(Context context) {
		super(context);
		init(context);
	}
	
	public MenuView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	
	public MenuView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	
	private void init(Context context) {
		mContext = context;
		LayoutInflater.from(context).inflate(R.layout.menu, this, true);
		setLayoutParams(new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
		initView();
	}
	
	private void initView() {
		initData();
		initMenuView();
	}

	private void initData() {
		list = new ArrayList<String>();
		for (int i=0; i<50; i++) {
			list.add("Ad" + String.format("%02d", i));
		}
	}

	private void initMenuView() {
		leftContent = (RelativeLayout) findViewById(R.id.left_content);
		btnClose = (ImageView) findViewById(R.id.btn_close);
		btnClose.setOnClickListener(this);
		btnMain = (ImageView) findViewById(R.id.btn_main);
		btnMain.setOnClickListener(this);
		btnReplay = (ImageView) findViewById(R.id.btn_replay);
		btnReplay.setOnClickListener(this);
		btnMusic = (ImageView) findViewById(R.id.btn_music);
		btnMusic.setOnClickListener(this);
		btnSound = (ImageView) findViewById(R.id.btn_sound);
		btnSound.setOnClickListener(this);
		

		RightContent = (RelativeLayout) findViewById(R.id.right_content);
		lstvAds = (ListView) findViewById(R.id.ads);
		adapter = new AdsAdapter(list);
		lstvAds.setAdapter(adapter);

		animShow = AnimationUtils.loadAnimation(mContext, R.anim.show_menu);
		animHide = AnimationUtils.loadAnimation(mContext, R.anim.hide_menu);
		animFadeIn = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
		animFadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out);
		
		setOnTouchListener(this);
	}
	
	public void setOnButtonClickListener(OnButtonClickListener listener) {
		mOnButtonClickListener = listener;
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return true;
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_close: {
			close();
		}
			break;
		case R.id.btn_main: {
			mainMenu();
		}
			break;
		case R.id.btn_replay: {
			replay();
		}
			break;
		case R.id.btn_music: {
			musicSwitch();
		}
			break;
		case R.id.btn_sound: {
			soundSwitch();
		}
			break;
		}
	}
	
	private void close() {
		hideMenu();
		mOnButtonClickListener.onClose();
	}

	private void mainMenu() {
		mOnButtonClickListener.onMainMenu();
	}

	private void replay() {
		mOnButtonClickListener.onReplay();
	}
	
	private void musicSwitch() {
		if (btnMusic.isSelected()) {
			btnMusic.post(new Runnable() {
				@Override
				public void run() {
					btnMusic.setSelected(false);
				}
			});
		} else {
			btnMusic.post(new Runnable() {
				@Override
				public void run() {
					btnMusic.setSelected(true);
				}
			});
		}
		mOnButtonClickListener.onMusicSwitch();
	}
	
	private void soundSwitch() {
		if (btnSound.isSelected()) {
			btnSound.post(new Runnable() {
				@Override
				public void run() {
					btnSound.setSelected(false);
				}
			});
		} else {
			btnSound.post(new Runnable() {
				@Override
				public void run() {
					btnSound.setSelected(true);
				}
			});
		}
		mOnButtonClickListener.onSoundSwitch();
	}

	private void showMenu() {
		this.setVisibility(View.VISIBLE);
		leftContent.startAnimation(animShow);
	}

	private void hideMenu() {
		leftContent.startAnimation(animHide);
		this.postDelayed(new Runnable() {
			@Override
			public void run() {
				MenuView.this.setVisibility(View.GONE);
			}
		}, 300);
	}
	
	public void show() {
		showMenu();
	}
	
	public void hide() {
		hideMenu();
	}
	
	private class AdsAdapter extends BaseAdapter {
		private ArrayList<String> list;

		public AdsAdapter(ArrayList<String> list) {
			this.list = list;
		}
		
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			AdHolder holder;
			
			if (convertView == null) {
				holder = new AdHolder();
				convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_ad, null);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				convertView.setTag(holder);
			} else {
				holder = (AdHolder) convertView.getTag();
			}
			
			holder.name.setText(list.get(position));
			
			return convertView;
		}
		
	}
	
	static class AdHolder {
		TextView name;
	}

	public interface OnButtonClickListener {
		public void onClose();
		public void onReplay();
		public void onMainMenu();
		public void onMusicSwitch();
		public void onSoundSwitch();
	}
}
